<?php if (!defined('BASEPATH')) exit('No direct script access allowed');

class Admin_Mediatypesettings extends Admin_Controller {
	public function __construct()
	{
		parent::__construct();
		$this->lang->load('mediatypesetting');
	}
	public function ajax_mediatypesetting_get(){
		$mediatypesettingEntity =  new CI_Media_type_setting_entity();
		$mediatypesetting = $mediatypesettingEntity->getMediaTypeSettingByCode($_REQUEST['code']);
		echo json_encode($mediatypesetting);
	}
	public function ajax_mediatypesetting_list(){
		$mediatypesettingEntity =  new CI_Media_type_setting_entity();
		$escortSettings = new CI_Escort_setting_entity();
		$defaultLanguageId = $escortSettings->getDefaultLanguageId();
		$mediatypesettingList = $mediatypesettingEntity->getMediaTypeSettingListByLanguage($defaultLanguageId);
		$data['mediatypesettingList'] = $mediatypesettingList;
		
		$languageEntity = new CI_Language_entity();
		$languageList = $languageEntity->getLanguageList();
		$data['languageList'] = $languageList;
		
		$this->load->view('admin/mediatypesettings/index', $data);
	}
	public function ajax_mediatypesetting_save(){
		$mediatypesettingNameList = $_REQUEST['mediatypesetting'];
		$mediatypesettingNameDefault = '';
		foreach ($mediatypesettingNameList as $key => $value) {
			if($value != null && $value != ""){
				$mediatypesettingNameDefault = $value;
				break;
			}
		}
		$result = array();
		if($mediatypesettingNameDefault == ''){
			$result['result'] = false;
			$result['message'] = lang('escort_mediatypesetting_require');
		}else{
			$code = 0;
			foreach ($mediatypesettingNameList as $key => $value) {
				$mediatypesettingEntity =  new CI_Media_type_setting_entity();
				if($value != null && $value != ''){
					$mediatypesettingEntity->setMediaType($value);
				}else{
					$mediatypesettingEntity->setMediaType($mediatypesettingNameDefault);
				}
				$mediatypesettingEntity->setLanguageId($key);
				$mediatypesettingEntity->setCode($code);
				$id = $mediatypesettingEntity->save();
				if($code == 0){
					$code = $id;
					$mediatypesettingEntity->setId($id);
					$mediatypesettingEntity->setCode($code);
					$mediatypesettingEntity->update();
				}
			}
			$result['result'] = true;
			$result['message'] =lang('escort_mediatypesetting_save_success');
			$result['id'] =$id;
		}
		echo json_encode($result);
	}
	public function ajax_mediatypesetting_update(){
		$mediatypesettingNameList = $_REQUEST['mediatypesetting'];
		$mediatypesettingIdList = $_REQUEST['mediatypesetting_id'];
		$mediatypesettingCode = $_REQUEST['mediatypesetting_code'];
		$mediatypesettingNameDefault = '';
		foreach ($mediatypesettingNameList as $key => $value) {
			if($value != null && $value != ""){
				$mediatypesettingNameDefault = $value;
				break;
			}
		}
		$result = array();
		if($mediatypesettingNameDefault == ''){
			$result['result'] = false;
			$result['message'] = lang('escort_mediatypesetting_require');
		}else{
			foreach ($mediatypesettingNameList as $key => $value) {
				$mediatypesettingEntity =  new CI_Media_type_setting_entity();
				if($value != null && $value != ''){
					$mediatypesettingEntity->setMediaType($value);
				}else{
					$mediatypesettingEntity->setMediaType($mediatypesettingNameDefault);
				}
				$mediatypesettingEntity->setLanguageId($key);
				$mediatypesettingEntity->setCode($mediatypesettingCode);
				$mediatypesettingEntity->setId($mediatypesettingIdList[$key]);
				$mediatypesettingEntity->update();
			}
			$result['result'] = true;
			$result['message'] =lang('escort_mediatypesetting_save_success');
		}
		
		echo json_encode($result);
	}
	public function ajax_mediatypesetting_delete(){
		$result = array();
		$mediatypesettingEntity =  new CI_Media_type_setting_entity();
		$result['result'] = $mediatypesettingEntity->deleteByCode($_REQUEST['code']);
		if($result['result']){
			$result['message'] = lang('escort_mediatypesetting_delete_success');
		}else{
			$result['message'] = lang('escort_mediatypesetting_delete_error');
		}
		echo json_encode($result);
	}
}
?>